package cn.brilliantfuture.commentry.operate.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.ejb.Stateless;
import javax.jws.WebService;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.operate.QueryMessageRemote;
import cn.brilliantfuture.commentry.operate.QueryMessageWebService;
import cn.brilliantfuture.commentry.util.BaseMessageImpl;
import cn.brilliantfuture.commentry.util.HttpUtil;

/**
 * 
 * Title: Communication Entry
 * 
 * Author: LiTian
 * 
 * Date: 2011-11-15
 * 
 * Description: 查询信息类
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.operate.QueryMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/QueryMessageWebService")
public class QueryMessageImpl extends BaseMessageImpl implements QueryMessageWebService, QueryMessageRemote {

	public String queryAdMsg(String deviceSn, String queryKey) {
		StringBuffer sb = new StringBuffer();
		sb.append(getRemoteUrl());
		sb.append("/query/?cabinetID=");
		sb.append(deviceSn);
		sb.append("&queryKey=");
		sb.append(queryKey);
		logger.info(sb.toString());
		String value = HttpUtil.getInvoke(sb.toString()).replace("\r\n", "");

		try {
			value = URLDecoder.decode(value, "utf8");
		} catch (UnsupportedEncodingException e) {
			value = "";
			logger.error(e.getMessage());
		}
		return value;
	}

	public String queryRemoteState(String deviceSn, String queryKey, String gridId) {
		StringBuffer sb = new StringBuffer();
		sb.append(getRemoteUrl());
		sb.append("/query/xml?cabinetID=");
		sb.append(deviceSn);
		sb.append("&queryKey=");
		sb.append(queryKey);

		if (QueryMessageRemote.ONE_BOX_STATUS.equals(queryKey)) {
			sb.append("&gridId=");
			sb.append(gridId);
		}
		logger.info(sb.toString());
		return HttpUtil.getInvoke(sb.toString());
	}

	public String queryOnlineDevice(String deviceSn) {
		StringBuffer sb = new StringBuffer();
		sb.append(getRemoteUrl());
		sb.append("/query/online");
		
		if (null != deviceSn)
			sb.append("?deviceSn=").append(deviceSn);

		logger.info(sb.toString());
		return HttpUtil.getInvoke(sb.toString());
	}
}
